﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Data;
using ZHDJ.Data.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.Web
{
    public partial class ShowDayRubricInfo : DBPageBase
    {
        /// <summary>
        /// 题目ID，第一题
        /// </summary>
        public string RubricID { get; set; }

        /// <summary>
        /// 题目ID，第二题
        /// </summary>
        public string RubricID1 { get; set; }

        /// <summary>
        /// 题目ID，第三题
        /// </summary>
        public string RubricID2 { get; set; }

        /// <summary>
        /// 题目类型(单选、多选、判断等)，第一题
        /// </summary>
        public string RubricType { get; set; }

        /// <summary>
        /// 题目类型(单选、多选、判断等)，第二题
        /// </summary>
        public string RubricType1 { get; set; }

        /// <summary>
        /// 题目类型(单选、多选、判断等)，第三题
        /// </summary>
        public string RubricType2 { get; set; }

        /// <summary>
        /// 正确答案，第一题
        /// </summary>
        public string OKAnswer { get; set; }

        /// <summary>
        /// 正确答案，第二题
        /// </summary>
        public string OKAnswer1 { get; set; }

        /// <summary>
        /// 正确答案，第三题
        /// </summary>
        public string OKAnswer2 { get; set; }

        /// <summary>
        /// 第一题答案正确 
        /// </summary>
        public bool TheAnswerRight{ get; set; }

        /// <summary>
        /// 第二题答案正确 
        /// </summary>
        public bool TheAnswerRight1 { get; set; }

        /// <summary>
        /// 第三题答案正确 
        /// </summary>
        public bool TheAnswerRight2 { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            
            CheckLogin(true, true);
            
            
            //每日一题判断是否重复答题
            var SqlServer = @"select * from dayrecored where CONVERT(varchar(100), GETDATE(), 23)=CONVERT(varchar(100), InputTime, 23) and UserCode='" + CurrentUser.IdentityCard + "' order by InputTime desc;";
            int iii = SqlServerHelper.ExecuteDataTable(SqlServer).Rows.Count;
            if (iii > 0)
            {
                //Response.Write("<script>alert('今天已经答过题了，清不要重复答题！')</script>");
                Response.Redirect("/Default.aspx", false);
                //butDefault.Text = "确定";
                //butDefault.OnClientClick = "closeFrame()";
                //DataSet Rubric = SqlServerHelper.ExecuteDataSet(SqlServer);
                //LoadData(Rubric);
                return;
            }
            OKAnswer = labOKMemo.Text;
            OKAnswer1 = labOKMemo1.Text;
            OKAnswer2 = labOKMemo2.Text;

            RubricType = labType.Text;
            RubricType1 = labType1.Text;
            RubricType2 = labType2.Text;

            RubricID = labID.Text;
            RubricID1 = labID1.Text;
            RubricID2 = labID2.Text;

            TheAnswerRight = false;
            TheAnswerRight1 = false;
            TheAnswerRight2 = false;
            if (!IsPostBack)
            {
                var sql = @"select top 3 * from dbo.RubricInfo where Flag='E' and RubricType in ('A','B','C') order by newid();";
                DataSet Rubric = SqlServerHelper.ExecuteDataSet(sql);
                LoadData(Rubric);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //第一题
            if (RubricType == "A")
            {
                if (RBLAData.Text.Substring(0, 1) == OKAnswer)
                {
                    BindData("1", OKAnswer, "Y");
                }
                else
                {
                    BindData("1", OKAnswer, "N");
                }
            }

            if (RubricType=="B") {
                string str = "";
                for (int i = 0; i < CBLBData.Items.Count; i++)
                {
                    if (CBLBData.Items[i].Selected)
                    {
                        str = str + CBLBData.Items[i].Value.Substring(0,1) + "#";
                    }
                }
                //对str进行处理，除去最后一个‘,’
                string subStr = str.Substring(0, str.LastIndexOf('#'));
                if (subStr == OKAnswer)
                {
                    BindData("1", OKAnswer, "Y");
                }
                else {
                    BindData("1", OKAnswer, "N");
                }
            }
            
            if (RubricType == "C")
            {
                string Yn = "";
                if (RBLAData.Text == "正确") {
                    Yn = "Y";
                }
                if (RBLAData.Text == "错误") {
                    Yn = "N";
                }
                if (Yn == OKAnswer)
                {
                    BindData("1", "正确", "Y");
                }
                else
                {
                    BindData("1", "错误", "Y");
                }
            }

            //第二题
            if (RubricType1 == "A")
            {
                if (RBLAData1.Text.Substring(0, 1) == OKAnswer1)
                {
                    BindData("2", OKAnswer1, "Y");
                }
                else
                {
                    BindData("2", OKAnswer1, "N");
                }
            }

            if (RubricType1 == "B")
            {
                string str = "";
                for (int i = 0; i < CBLBData1.Items.Count; i++)
                {
                    if (CBLBData1.Items[i].Selected)
                    {
                        str = str + CBLBData1.Items[i].Value.Substring(0, 1) + "#";
                    }
                }
                //对str进行处理，除去最后一个‘,’
                string subStr = str.Substring(0, str.LastIndexOf('#'));
                if (subStr == OKAnswer1)
                {
                    BindData("2", OKAnswer1, "Y");
                }
                else
                {
                    BindData("2", OKAnswer1, "N");
                }
            }
            
            if (RubricType1 == "C")
            {
                string Yn = "";
                if (RBLAData1.Text == "正确")
                {
                    Yn = "Y";
                }
                if (RBLAData1.Text == "错误")
                {
                    Yn = "N";
                }
                if (Yn == OKAnswer1)
                {
                    BindData("2", "正确", "Y");
                }
                else
                {
                    BindData("2", "错误", "N");
                }
            }

            //第三题
            if (RubricType2 == "B")
            {
                string str = "";
                for (int i = 0; i < CBLBData2.Items.Count; i++)
                {
                    if (CBLBData2.Items[i].Selected)
                    {
                        str = str + CBLBData2.Items[i].Value.Substring(0, 1) + "#";
                    }
                }
                //对str进行处理，除去最后一个‘,’
                string subStr = str.Substring(0, str.LastIndexOf('#'));
                if (subStr == OKAnswer2)
                {
                    BindData("3", OKAnswer2, "Y");
                }
                else
                {
                    BindData("3", OKAnswer2, "N");
                }
            }
            if (RubricType2 == "A")
            {
                if (RBLAData2.Text.Substring(0, 1) == OKAnswer2)
                {
                    BindData("3", OKAnswer2, "Y");
                }
                else
                {
                    BindData("3", OKAnswer2, "N");
                }
            }
            if (RubricType2 == "C")
            {
                string Yn = "";
                if (RBLAData2.Text == "正确")
                {
                    Yn = "Y";
                }
                if (RBLAData2.Text == "错误")
                {
                    Yn = "N";
                }
                if (Yn == OKAnswer2)
                {
                    BindData("3", "正确", "Y");
                }
                else
                {
                    BindData("3", "错误", "N");
                }
            }
            string TheAnswerRight_cn = "错误";
            string TheAnswerRight_cn1 = "错误";
            string TheAnswerRight_cn2 = "错误";
            if (TheAnswerRight) {
                TheAnswerRight_cn = "正确";
            }
            if (TheAnswerRight1)
            {
                TheAnswerRight_cn1 = "正确";
            }
            if (TheAnswerRight2)
            {
                TheAnswerRight_cn2 = "正确";
            }

            if (butDefault.Text == "确定") {
                return;
            }

            butDefault.Text = "确定";
            butDefault.OnClientClick = "closeFrame()";

            string User = CurrentUser.LoginID;
            var tempSql = @" insert dayrecored (RubricID,IsOK,UserCode,UserName,InputTime) 
                values('" + RubricID + "','" + TheAnswerRight_cn + "','"+ CurrentUser.IdentityCard + "','" + ControlBusiness.GetMembersPickerText(User) + "','"+ DateTime.Now + "');";
            SqlServerHelper.ExecuteNonQuery(tempSql);
            var tempSql1 = @" insert dayrecored (RubricID,IsOK,UserCode,UserName,InputTime) 
                values('" + RubricID1 + "','" + TheAnswerRight_cn1 + "','" + CurrentUser.IdentityCard + "','" + ControlBusiness.GetMembersPickerText(User) + "','" + DateTime.Now + "');";
            SqlServerHelper.ExecuteNonQuery(tempSql1);
            var tempSql2 = @" insert dayrecored (RubricID,IsOK,UserCode,UserName,InputTime) 
                values('" + RubricID2 + "','" + TheAnswerRight_cn2 + "','" + CurrentUser.IdentityCard + "','" + ControlBusiness.GetMembersPickerText(User) + "','" + DateTime.Now + "');";
            SqlServerHelper.ExecuteNonQuery(tempSql2);
            if (TheAnswerRight && TheAnswerRight1 && TheAnswerRight2) {
                IncreaseTheIntegral();
            }
        }

        //数据加载
        private void BindData(string Fragenummer,string answer, string YN)
        {
            //Fragenummer:题号
            //answer:正确答案
            //YN:对错


            //第一题
            if (Fragenummer == "1") {
                if (YN == "Y") {
                    ImgOKAnswerS.Visible = true;
                    labOKMemo.Visible = true;
                    labOKMemo.Text = "正确答案: " + answer;
                    labOKMemo.ForeColor = Color.Black;
                    ImgOKAnswerS.ImageUrl = "image/WriteY.gif";
                    TheAnswerRight = true;
                }
                if (YN == "N")
                {
                    ImgOKAnswerS.Visible = true;
                    labOKMemo.Visible = true;
                    labOKMemo.Text = "正确答案: " + answer;
                    labOKMemo.ForeColor = Color.Red;
                    ImgOKAnswerS.ImageUrl = "image/WriteN.gif";
                }
            }
            //第二题
            if (Fragenummer == "2")
            {
                if (YN == "Y")
                {
                    ImgOKAnswerS1.Visible = true;
                    labOKMemo1.Visible = true;
                    labOKMemo.Text = "正确答案: " + answer;
                    labOKMemo1.ForeColor = Color.Black;
                    ImgOKAnswerS1.ImageUrl = "image/WriteY.gif";
                    TheAnswerRight1 = true;
                }
                if (YN == "N")
                {
                    ImgOKAnswerS1.Visible = true;
                    labOKMemo1.Visible = true;
                    labOKMemo1.Text = "正确答案: " + answer;
                    labOKMemo1.ForeColor = Color.Red;
                    ImgOKAnswerS1.ImageUrl = "image/WriteN.gif";
                }
            }
            //第三题
            if (Fragenummer == "3")
            {
                if (YN == "Y")
                {
                    ImgOKAnswerS2.Visible = true;
                    labOKMemo2.Visible = true;
                    labOKMemo2.Text = "正确答案: " + answer;
                    labOKMemo2.ForeColor = Color.Black;
                    ImgOKAnswerS2.ImageUrl = "image/WriteY.gif";
                    TheAnswerRight2 = true;
                }
                if (YN == "N")
                {
                    ImgOKAnswerS2.Visible = true;
                    labOKMemo2.Visible = true;
                    labOKMemo2.Text = "正确答案: " + answer;
                    labOKMemo2.ForeColor = Color.Red;
                    ImgOKAnswerS2.ImageUrl = "image/WriteN.gif";
                }
            }
        }


        //数据绑定
        private void LoadData(DataSet Rubric) {
            labRubricTitle.Text = Convert.ToString(Rubric.Tables[0].Rows[0]["RubricTitle"].ToString());
            labOpertion.Text = Convert.ToString(Rubric.Tables[0].Rows[0]["OptionAnswer"].ToString());
            labType.Text = Convert.ToString(Rubric.Tables[0].Rows[0]["RubricType"].ToString());
            labID.Text = Convert.ToString(Rubric.Tables[0].Rows[0]["ID"].ToString());
            labOKMemo.Text = Convert.ToString(Rubric.Tables[0].Rows[0]["OKAnswer"].ToString());

            labRubricTitle1.Text = Convert.ToString(Rubric.Tables[0].Rows[1]["RubricTitle"].ToString());
            labOpertion1.Text = Convert.ToString(Rubric.Tables[0].Rows[1]["OptionAnswer"].ToString());
            labType1.Text = Convert.ToString(Rubric.Tables[0].Rows[1]["RubricType"].ToString());
            labID1.Text = Convert.ToString(Rubric.Tables[0].Rows[1]["ID"].ToString());
            labOKMemo1.Text = Convert.ToString(Rubric.Tables[0].Rows[1]["OKAnswer"].ToString());

            labRubricTitle2.Text = Convert.ToString(Rubric.Tables[0].Rows[2]["RubricTitle"].ToString());
            labOpertion2.Text = Convert.ToString(Rubric.Tables[0].Rows[2]["OptionAnswer"].ToString());
            labType2.Text = Convert.ToString(Rubric.Tables[0].Rows[2]["RubricType"].ToString());
            labID2.Text = Convert.ToString(Rubric.Tables[0].Rows[2]["ID"].ToString());
            labOKMemo2.Text = Convert.ToString(Rubric.Tables[0].Rows[2]["OKAnswer"].ToString());

            //第一题

            string[] a = labOpertion.Text.Split('#');
            if (labType.Text == "A")
            {
                RBLAData.Items.Add("A. " + a[0]);
                RBLAData.Items.Add("B. " + a[1]);
                RBLAData.Items.Add("C. " + a[2]);
                if (a[3] != "")
                {
                    RBLAData.Items.Add("D. " + a[3]);
                }
            }
            if (labType.Text == "B")
            {
                CBLBData.Items.Add("A. " + a[0]);
                CBLBData.Items.Add("B. " + a[1]);
                CBLBData.Items.Add("C. " + a[2]);
                if (a[3] != "")
                {
                    CBLBData.Items.Add("D. " + a[3]);
                }
            }
            if (labType.Text == "C")
            {
                RBLAData.Items.Add("正确");
                RBLAData.Items.Add("错误");
            }

            //第二题

            string[] a1 = labOpertion1.Text.Split('#');
            if (labType1.Text == "A")
            {
                RBLAData1.Items.Add("A. " + a1[0]);
                RBLAData1.Items.Add("B. " + a1[1]);
                RBLAData1.Items.Add("C. " + a1[2]);
                if (a1[3] != "")
                {
                    RBLAData1.Items.Add("D. " + a1[3]);
                }
            }
            if (labType1.Text == "B")
            {
                CBLBData1.Items.Add("A. " + a1[0]);
                CBLBData1.Items.Add("B. " + a1[1]);
                CBLBData1.Items.Add("C. " + a1[2]);
                if (a1[3] != "")
                {
                    CBLBData1.Items.Add("D. " + a1[3]);
                }
            }
            if (labType1.Text == "C")
            {
                RBLAData1.Items.Add("正确");
                RBLAData1.Items.Add("错误");
            }

            //第三题

            string[] a2 = labOpertion2.Text.Split('#');
            if (labType2.Text == "A")
            {
                RBLAData2.Items.Add("A. " + a2[0]);
                RBLAData2.Items.Add("B. " + a2[1]);
                RBLAData2.Items.Add("C. " + a2[2]);
                if (a2[3] != "")
                {
                    RBLAData2.Items.Add("D. " + a2[3]);
                }
            }
            if (labType2.Text == "B")
            {
                CBLBData2.Items.Add("A. " + a2[0]);
                CBLBData2.Items.Add("B. " + a2[1]);
                CBLBData2.Items.Add("C. " + a2[2]);
                if (a2[3] != "")
                {
                    CBLBData2.Items.Add("D. " + a2[3]);
                }
            }
            if (labType2.Text == "C")
            {
                RBLAData2.Items.Add("正确");
                RBLAData2.Items.Add("错误");
            }
        }

        public void IncreaseTheIntegral() {
            //增加积分
            PartyScoreHelper.Add(() => PartyScoreHelper.AddEverydaylearning(CurrentUser.LoginID));
        }
    }
}